<?php
/**
 * Created by Phpstorm
 * User: 一个猫
 * Date: 2023/11/22
 * Time: 16:21
 */

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\AddSubordinateRequest;
use App\Http\Requests\Api\ChangePasswordRequest;
use App\Models\BusinessMember;
use App\Models\MemberPosition;
use Illuminate\Support\Facades\Hash;

class MemberController extends Controller
{
    // 个人中心
    public function memberCenter()
    {
        // 用户信息
        $memberInfo = auth('api')->user();

//        // 获取可添加的数据，构建select满足数据结构
        $subordinate = MemberPosition::where('pid', '=', $memberInfo->position->level)
            ->select('position_name as text', 'id as value')
            ->get();
//
        return response()->json([
            'code' => 200,
            'message' =>'success',
            'data' => [
                'member' => [
                    'username' => $memberInfo->username,
                    'phone' => $memberInfo->phone,
                    'nickname' => $memberInfo->nickname,
                    'position' => $memberInfo->position->position_name
                ],
                'subordinate' => $subordinate
            ]
        ]);
    }

    // 添加下级
    public function addSubordinate(AddSubordinateRequest $request)
    {
        // 参数验证
        $validated = $request->validated();

        // 数据保存
        $businessMember = new BusinessMember();
        $businessMember->username = $validated['mobile'];
        $businessMember->nickname = $validated['nickname'];
        $businessMember->password = Hash::make($validated['password']);
        $businessMember->company_name = $request->company_name;
        $businessMember->company_tel = $request->company_tel;
        $businessMember->position_id = $validated['value'];
        $businessMember->parent_id = auth('api')->id();
        $businessMember->phone = $validated['mobile'];

        if ($businessMember->save()) {
            return response()->json([
                'code' => 200,
               'message' =>'success',
                'data' => []
            ]);
        }

        return response()->json([
            'code' => -1,
            'message' =>'失败！',
            'data' => []
        ]);
    }

    // 修改密码
    public function changePassword(ChangePasswordRequest $request)
    {
        // 已验证数据获取
        $validated = $request->validated();

        try {
            if ((new BusinessMember())->changePassword($validated['old_password'], $validated['password'])) {
                return response()->json([
                    'code' => 200,
                    'message' =>'success',
                    'data' => []
                ]);
            }
        } catch (\Exception $e) {
            $errMsg = $e->getMessage();
        }

        return response()->json([
            'code' => -1,
            'message' => $errMsg ?? '修改失败！',
            'data' => []
        ]);
    }

    // 退出登录
    public function logout()
    {
        auth('api')->logout();
        return response()->json([
            'code' => 200,
            'message' =>'success',
            'data' => []
        ]);
    }
}
